package com.honghe.config;


import com.honghe.interceptor.JwtDoctorInterceptor;
import com.honghe.interceptor.JwtPatientInterceptor;
import com.honghe.json.JacksonObjectMapper;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


@Configuration
@Slf4j
public class WebConfiguration implements WebMvcConfigurer{
    @Autowired
    private JwtDoctorInterceptor jwtDoctorInterceptor;
    @Autowired
    private JwtPatientInterceptor jwtPatientInterceptor;

    @Bean
    public OpenAPI springShopOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("医院挂号系统")
                        .description("医院挂号系统项目")
                        .version("v1")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("外部文档")
                        .url("https://springshop.wiki.github.org/docs"));
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtDoctorInterceptor)
                .addPathPatterns("/doctor/**")
                .excludePathPatterns("/doctor/doctor/login");
        registry.addInterceptor(jwtPatientInterceptor)
                .addPathPatterns("/patient/**")
                .excludePathPatterns("/patient/login")
                .excludePathPatterns("/patient/add");
    }
    /**
     * 扩展springMVC消息转换器
     * @param converters
     * */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转换器...");
        //创建一个消息转换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //为消息转换器设置一个对象转换器,对象转换器可以将java序列化为一个json数据
        converter.setObjectMapper(new JacksonObjectMapper());
        //将自己的消息转换器加入到容器中
        converters.add(2,converter);
    }
}
